<template>
  <div>
    <badge :count="count"></badge>
  </div>
</template>

<script>
  import Badge from '@/components/Badge'
  import { getShopCartVeekray } from '@/api/shop'
  import { setFastGoods } from '@/utils/auth'

  export default {
    components: { Badge },
    data() {
      return {
        isComponents: true,
        count: 0,
        tempCount: 0,
        listQuery: {
          memberId: '',
          customerId: '',
          enterpriseId: ''
        },
        promotionList: [],
        fastGoods: []
      }
    },
    onShow() {
      // this.initCount()
    },
    mounted() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
      this.initCount()
    },
    methods: {
      initCount() {
        if (this.getCustomerId) {
          this.listQuery.memberId = this.getMemberId
          this.listQuery.customerId = this.getCustomerId
          this.listQuery.enterpriseId = this.getEnterpriseId ? this.getEnterpriseId : ''
          this.getShopCartVeekray()
        }
      },
      getShopCartVeekray() {
        this.promotionList = []
        getShopCartVeekray(this.listQuery).then(response => {
          if (response.isError) return
          this.$pt.hideToast()
          if (response.data.promotionList) {
            this.promotionList = response.data.promotionList
          } else {
            this.promotionList = []
          }
          let listRes = response.data.effectiveList
          // 社区团购商品列表
          let groupBuyGoodsList = response.data.groupBuyGoodsList
          // 门店商品列表
          let storeHomeGoodsList = response.data.storeHomeGoodsList
          this.$pt.removeStorageSync('fastGoods')
          this.fastGoods = []
          let allEffectiveList = []
          let promotionList = []
          this.promotionList.forEach(item => {
            item.feeGoodsList.forEach(itemSub => {
              promotionList.push(itemSub)
            })
          })
          allEffectiveList = listRes.concat(promotionList, groupBuyGoodsList, storeHomeGoodsList)
          if (allEffectiveList.length) {
            allEffectiveList.forEach((item, index) => {
              // 重新获取快速购买数据
              if (item.skuId) {
              // 多规格
                let skuData = []
                skuData.push({
                  skuId: item.skuId,
                  goodsNum: item.goodsNum,
                  sellPrice: (Math.round((item.goodsNum * item.sellPrice) * 100) / 100).toFixed(2)
                })
                let goodsNum = null
                let totalSellPrice = null
                skuData.forEach(item => {
                  goodsNum += item.goodsNum
                  totalSellPrice += Math.round(item.sellPrice * 100) / 100
                })
                this.fastGoods.push({
                  goodsId: item.goodsId,
                  skuFlag: 1,
                  skuList: skuData,
                  goodsNum: goodsNum,
                  totalSellPrice: (Math.round(totalSellPrice * 100) / 100).toFixed(2)
                })
              } else {
                // 单规格
                this.fastGoods.push({
                  goodsId: item.goodsId,
                  skuFlag: 0,
                  goodsNum: item.goodsNum,
                  sellPrice: (Math.round((item.goodsNum * item.sellPrice) * 100) / 100).toFixed(2)
                })
              }
            })
          }
          let map = {}
          let dest = []
          this.fastGoods.forEach(item => {
            if (!map[item.goodsId]) {
              if (item.skuFlag) {
                dest.push({
                  goodsId: item.goodsId,
                  goodsNum: item.goodsNum,
                  totalSellPrice: item.totalSellPrice,
                  skuFlag: 1,
                  skuList: item.skuList
                })
              } else {
                dest.push({
                  goodsId: item.goodsId,
                  goodsNum: item.goodsNum,
                  sellPrice: item.sellPrice,
                  skuFlag: 0,
                  skuList: item.skuList
                })
              }
              map[item.goodsId] = item
            } else {
              dest.forEach(itemSub => {
                if (itemSub.goodsId === item.goodsId) {
                  itemSub.goodsNum = itemSub.goodsNum + item.goodsNum
                  itemSub.totalSellPrice = (Math.round(itemSub.totalSellPrice * 100) / 100 + Math.round(item.totalSellPrice * 100) / 100).toFixed(2)
                  itemSub.skuList = itemSub.skuList.concat(item.skuList)
                }
              })
            }
          })
          this.fastGoods = []
          this.fastGoods = dest
          setFastGoods(this.fastGoods)
          // let cartBadgeParams = this.$pt.getStorageSync('skuParams')
          this.tempCount = 0
          this.fastGoods.forEach(itemGoods => {
            // if (itemGoods.goodsId === cartBadgeParams.goodsId) {
            //   this.count = itemGoods.goodsNum
            // }
            this.tempCount += itemGoods.goodsNum
          })
          this.count = this.tempCount
        }).catch(() => {
        })
      }
    }
  }
</script>

<style scoped>

</style>
